package fr.umlv.m2.android.quizz.scores;

import java.sql.Time;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import fr.umlv.m2.android.quizz.R;
import fr.umlv.m2.android.quizz.database.Score;
import fr.umlv.m2.android.quizz.database.ScoreDb;
import fr.umlv.m2.android.quizz.database.UserDb;

public class ScoresSinglePlayerByThemeActivity extends Activity {
	/**
	 * The best player.
	 */
	private TextView player1;
	/**
	 * The number of good answer in the best game.
	 */
	private TextView goodAnswerText1;
	/**
	 * The number of bad answer in the best game.
	 */
	private TextView badAnswerText1;
	/**
	 * The percentage of success in the best game.
	 */
	private TextView SuccessPercentageText1;
	/**
	 * The time to end the best game.
	 */
	private TextView timeText1;
	/**
	 * The second best player.
	 */
	private TextView player2;
	/**
	 * The number of good answer in the second best game.
	 */
	private TextView goodAnswerText2;
	/**
	 * The number of bad answer in the second best game.
	 */
	private TextView badAnswerText2;
	/**
	 * The percentage of success in the second best game.
	 */
	private TextView SuccessPercentageText2;
	/**
	 * The time to end the second best game.
	 */
	private TextView timeText2;
	/**
	 * The third best player.
	 */
	private TextView player3;
	/**
	 * The number of good answer in the third best game.
	 */
	private TextView goodAnswerText3;
	/**
	 * The number of bad answer in the third best game.
	 */
	private TextView badAnswerText3;
	/**
	 * The percentage of success in the third best game.
	 */
	private TextView SuccessPercentageText3;
	/**
	 * The time to end the third best game.
	 */
	private TextView timeText3;
	
	/**
	 * The seconds of the time.
	 */
	private int seconde;
	/**
	 * The minutes of the time.
	 */
	private int minutes;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.scores_single_player_by_theme);
		
		final Intent intent = this.getIntent();
	    final Bundle bundle = intent.getExtras();
	    final long levelId = bundle.getLong("levelId");
	    final long themeId = bundle.getLong("themeId");
		
	    this.player1 = (TextView)findViewById(R.id.scoresSinglePlayerBestPlayer);
		this.goodAnswerText1 = (TextView)findViewById(R.id.scoresSinglePlayerGoodAnswerText1);
		this.badAnswerText1 = (TextView)findViewById(R.id.scoresSinglePlayerBadAnswerText1);
		this.SuccessPercentageText1 = (TextView)findViewById(R.id.scoresSinglePlayerSuccessPercentageText1);
		this.timeText1 = (TextView)findViewById(R.id.scoresSinglePlayerTimeText1);
		
		this.player2 = (TextView)findViewById(R.id.scoresSinglePlayerSecondPlayer);
		this.goodAnswerText2 = (TextView)findViewById(R.id.scoresSinglePlayerGoodAnswerText2);
		this.badAnswerText2 = (TextView)findViewById(R.id.scoresSinglePlayerBadAnswerText2);
		this.SuccessPercentageText2 = (TextView)findViewById(R.id.scoresSinglePlayerSuccessPercentageText2);
		this.timeText2 = (TextView)findViewById(R.id.scoresSinglePlayerTimeText2);
		
		this.player3 = (TextView)findViewById(R.id.scoresSinglePlayerThirdPlayer);
		this.goodAnswerText3 = (TextView)findViewById(R.id.scoresSinglePlayerGoodAnswerText3);
		this.badAnswerText3 = (TextView)findViewById(R.id.scoresSinglePlayerBadAnswerText3);
		this.SuccessPercentageText3 = (TextView)findViewById(R.id.scoresSinglePlayerSuccessPercentageText3);
		this.timeText3 = (TextView)findViewById(R.id.scoresSinglePlayerTimeText3);
		
		ScoreDb scoreDb = new ScoreDb(this);
		scoreDb.open();
		
		if(scoreDb.getNbScore(levelId, themeId) > 0) {
			List<Score> scores = scoreDb.getSinglePlayerScores(levelId, themeId);
			UserDb userDb = new UserDb(this);
			userDb.open();
			
			this.player1.setText(userDb.getUserWithId(scores.get(0).getUserId()).getName());
			this.goodAnswerText1.setText(String.valueOf(scores.get(0).getGoodAnswer()));
			this.badAnswerText1.setText(String.valueOf(scores.get(0).getBadAnswer()));
			this.SuccessPercentageText1.setText(scores.get(0).getSuccessPercentage() + "%");
			
			Time time = new Time(scores.get(0).getTime());
			this.seconde = time.getSeconds();
			this.minutes = time.getMinutes();
			this.timeText1.setText(
					((this.minutes < 10) ? "0"+this.minutes : this.minutes) +
					":" +
					((this.seconde < 10) ? "0"+this.seconde : this.seconde)	
			);
			
			if(scoreDb.getNbScore(levelId, themeId) > 1) {
				this.player2.setText(userDb.getUserWithId(scores.get(1).getUserId()).getName());
				this.goodAnswerText2.setText(String.valueOf(scores.get(1).getGoodAnswer()));
				this.badAnswerText2.setText(String.valueOf(scores.get(1).getBadAnswer()));
				this.SuccessPercentageText2.setText(scores.get(1).getSuccessPercentage() + "%");
	
				time = new Time(scores.get(1).getTime());
				this.seconde = time.getSeconds();
				this.minutes = time.getMinutes();
				this.timeText2.setText(
						((this.minutes < 10) ? "0"+this.minutes : this.minutes) +
						":" +
						((this.seconde < 10) ? "0"+this.seconde : this.seconde)	
				);
			}
			
			if(scoreDb.getNbScore(levelId, themeId) > 2) {
				this.player3.setText(userDb.getUserWithId(scores.get(2).getUserId()).getName());
				this.goodAnswerText3.setText(String.valueOf(scores.get(2).getGoodAnswer()));
				this.badAnswerText3.setText(String.valueOf(scores.get(2).getBadAnswer()));
				this.SuccessPercentageText3.setText(scores.get(2).getSuccessPercentage() + "%");
	
				time = new Time(scores.get(2).getTime());
				this.seconde = time.getSeconds();
				this.minutes = time.getMinutes();
				this.timeText3.setText(String.valueOf(
						((this.minutes < 10) ? "0"+this.minutes : this.minutes) +
						":" +
						((this.seconde < 10) ? "0"+this.seconde : this.seconde)
				));
			}
			
			userDb.close();
		}

		scoreDb.close();
	}
}
